package com.qinghuan.mvc.controller;

import com.qinghuan.mvc.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: dubao
 * @Date: 2022/4/3 12:20
 * @Description:
 */
@Controller
public class HttpController {

    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String requestBody) {
        System.out.println("requestBody:" + requestBody);
        return "success";
    }

    @RequestMapping("/testRequestEntity")
    public String testRequestEntity(RequestEntity<String> requestEntity) {
        //当前requestEntity表示整个请求报文的信息
        System.out.println("请求头：" + requestEntity.getHeaders());
        System.out.println("请求体：" + requestEntity.getBody());
        return "success";
    }

    /**
     * 原生servlet向浏览器响应数据
     */
    @RequestMapping("/testResponse")
    public void testResponse(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello,response");
    }

    @RequestMapping("/testResponseBody")
    /**
     * 加上这个注解以后，返回值不再是视图名称，而是当前响应的响应体
     */
    @ResponseBody
    public String testResponseBody() {
        return "success";
    }

    @RequestMapping("testResponseUser")
    @ResponseBody
    public User testResponseUser() {
        return new User(1001, "admin", "12345", 23, "男");
    }

    /**
     * ajax用法
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/testAxios")
    @ResponseBody
    public String testAxios(String username, String password) {
        System.out.println(username + "," + password);
        return "hello axios";
    }

}
